<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getOverviewApi } from '@/api/report'
import { ElMessage } from 'element-plus'
import { Loading, Reading, Calendar, User, Avatar, Document, TrendCharts } from '@element-plus/icons-vue'

const router = useRouter()

// 加载状态
const loading = ref(false)

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 获取概览数据
const getOverview = async () => {
  loading.value = true
  try {
    const result = await getOverviewApi()
    if (result.code === 1) {
      overviewData.value = result.data
    } else {
      ElMessage.error(result.msg || '获取概览数据失败')
    }
  } catch (error) {
    console.error('获取概览数据失败:', error)
    ElMessage.error('获取概览数据失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 快捷操作跳转
const handleQuickAction = (route) => {
  router.push(route)
}

// 初始化
onMounted(() => {
  getOverview()
})
</script>

<template>
  <div class="home-container">
    <!-- 欢迎信息 -->
    <div class="welcome-section">
      <h1 class="welcome-title">欢迎您, 进入轻客管家系统</h1>
    </div>

    <!-- 概览数据区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-panel">
        <h2 class="panel-title">线索概览</h2>
        <div v-if="loading" class="loading-container">
          <el-icon class="is-loading"><Loading /></el-icon>
          <span>加载中...</span>
        </div>
        <div v-else class="overview-grid">
          <div class="overview-card" :class="{ 'zero-value': overviewData.clueTotal === 0 }">
            <div class="card-title">总线索数</div>
            <div class="card-value">{{ overviewData.clueTotal }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.clueWaitAllot === 0 }">
            <div class="card-title">待分配线索数</div>
            <div class="card-value">{{ overviewData.clueWaitAllot }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.clueWaitFollow === 0 }">
            <div class="card-title">待跟进线索数</div>
            <div class="card-value">{{ overviewData.clueWaitFollow }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.clueFollowing === 0 }">
            <div class="card-title">跟进中线索数</div>
            <div class="card-value">{{ overviewData.clueFollowing }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.clueFalse === 0 }">
            <div class="card-title">伪线索数</div>
            <div class="card-value">{{ overviewData.clueFalse }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.clueConvertBusiness === 0 }">
            <div class="card-title">转商机线索数</div>
            <div class="card-value">{{ overviewData.clueConvertBusiness }}</div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-panel">
        <h2 class="panel-title">商机概览</h2>
        <div v-if="loading" class="loading-container">
          <el-icon class="is-loading"><Loading /></el-icon>
          <span>加载中...</span>
        </div>
        <div v-else class="overview-grid">
          <div class="overview-card" :class="{ 'zero-value': overviewData.businessTotal === 0 }">
            <div class="card-title">总商机数</div>
            <div class="card-value">{{ overviewData.businessTotal }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.businessWaitAllot === 0 }">
            <div class="card-title">待分配商机数</div>
            <div class="card-value">{{ overviewData.businessWaitAllot }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.businessWaitFollow === 0 }">
            <div class="card-title">待跟进商机数</div>
            <div class="card-value">{{ overviewData.businessWaitFollow }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.businessFollowing === 0 }">
            <div class="card-title">跟进中商机数</div>
            <div class="card-value">{{ overviewData.businessFollowing }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.businessFalse === 0 }">
            <div class="card-title">回收商机数</div>
            <div class="card-value">{{ overviewData.businessFalse }}</div>
          </div>
          <div class="overview-card" :class="{ 'zero-value': overviewData.businessConvertCustomer === 0 }">
            <div class="card-title">转客户商机数</div>
            <div class="card-value">{{ overviewData.businessConvertCustomer }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions-section">
      <div class="quick-actions-grid">
        <div class="quick-action-card" @click="handleQuickAction('/resource/course')">
          <el-icon class="action-icon"><Reading /></el-icon>
          <div class="action-title">课程管理</div>
          <div class="action-subtitle">课程信息维护</div>
        </div>
        <div class="quick-action-card" @click="handleQuickAction('/resource/activity')">
          <el-icon class="action-icon"><Calendar /></el-icon>
          <div class="action-title">活动管理</div>
          <div class="action-subtitle">营销活动管理</div>
        </div>
        <div class="quick-action-card" @click="handleQuickAction('/clue/list')">
          <el-icon class="action-icon"><User /></el-icon>
          <div class="action-title">线索管理</div>
          <div class="action-subtitle">潜在客户追踪</div>
        </div>
        <div class="quick-action-card" @click="handleQuickAction('/business/list')">
          <el-icon class="action-icon"><TrendCharts /></el-icon>
          <div class="action-title">商机管理</div>
          <div class="action-subtitle">销售机会管理</div>
        </div>
        <div class="quick-action-card" @click="handleQuickAction('/customer')">
          <el-icon class="action-icon"><Avatar /></el-icon>
          <div class="action-title">客户管理</div>
          <div class="action-subtitle">客户信息维护</div>
        </div>
        <div class="quick-action-card" @click="handleQuickAction('/system/log')">
          <el-icon class="action-icon"><Document /></el-icon>
          <div class="action-title">日志统计</div>
          <div class="action-subtitle">系统操作日志</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 欢迎区域 */
.welcome-section {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin: 0 0 15px 0;
}

/* 概览区域 */
.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.overview-panel {
  flex: 1;
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.panel-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0 0 20px 0;
  text-align: center;
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.overview-card {
  background-color: #f8f9fa;
  border-radius: 6px;
  padding: 15px;
  text-align: center;
  transition: all 0.3s ease;
}

.overview-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card-title {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
}

/* 为不同类型的卡片添加不同的颜色 */
.overview-card:nth-child(1) .card-value {
  color: #409EFF; /* 总数 - 蓝色 */
}

.overview-card:nth-child(2) .card-value {
  color: #E6A23C; /* 待分配 - 橙色 */
}

.overview-card:nth-child(3) .card-value {
  color: #F56C6C; /* 待跟进 - 红色 */
}

.overview-card:nth-child(4) .card-value {
  color: #67C23A; /* 跟进中 - 绿色 */
}

.overview-card:nth-child(5) .card-value {
  color: #909399; /* 伪线索/回收 - 灰色 */
}

.overview-card:nth-child(6) .card-value {
  color: #9C27B0; /* 转换 - 紫色 */
}

/* 零值卡片样式 */
.overview-card.zero-value {
  opacity: 0.6;
}

.overview-card.zero-value .card-value {
  color: #909399 !important;
}

/* 快捷操作区域 */
.quick-actions-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 20px;
}

.quick-action-card {
  background-color: #f8f9fa;
  border-radius: 6px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #e9ecef;
}

.action-icon {
  font-size: 32px;
  color: #409EFF;
  margin-bottom: 10px;
  display: block;
}

.quick-action-card:hover {
  background-color: #409EFF;
  color: #fff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.quick-action-card:hover .action-title,
.quick-action-card:hover .action-subtitle,
.quick-action-card:hover .action-icon {
  color: #fff;
}

.action-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.action-subtitle {
  font-size: 12px;
  color: #666;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #666;
}

.loading-container .el-icon {
  font-size: 24px;
  margin-bottom: 10px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
  }
  
  .quick-actions-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .overview-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .quick-actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .welcome-title {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .overview-grid {
    grid-template-columns: 1fr;
  }
  
  .quick-actions-grid {
    grid-template-columns: 1fr;
  }
}
</style>
